package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hee.erp.modules.entity.SysUserHealthReport;
import com.hee.erp.modules.mapper.SysUserHealthReportMapper;
import com.hee.erp.modules.service.ISysUserHealthReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.vo.UserContractListProperty;
import com.hee.erp.vo.UserHealthReportListProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-03-06
 */
@Service
@Transactional
public class SysUserHealthReportServiceImpl extends ServiceImpl<SysUserHealthReportMapper, SysUserHealthReport> implements ISysUserHealthReportService {

    @Override
    public List<SysUserHealthReport> listByUserId(String userId) {

        LambdaQueryWrapper<SysUserHealthReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserHealthReport::getUserId,userId);
        return this.list(queryWrapper);
    }

    @Override
    public List<SysUserHealthReport> saveUserHealthReport(List<SysUserHealthReport> list) {

        for (SysUserHealthReport sysUserHealthReport : list) {
            if (sysUserHealthReport.getId().contains("-")) {
                //新增
                sysUserHealthReport.setId(null);
                this.save(sysUserHealthReport);
            } else {
                //修改
                this.updateById(sysUserHealthReport);
            }
        }

        return list;
    }

    @Override
    public List<UserHealthReportListProperty> userHealthReportPropertyList(String userId) {

        List<SysUserHealthReport> healthReportList = this.listByUserId(userId);
        return this.convertToProperty(healthReportList);
    }

    @Override
    public List<UserHealthReportListProperty> saveUserHealthReportPropertyList(List<UserHealthReportListProperty> sourceList) {
        List<SysUserHealthReport> saveList = new ArrayList<>();
        for (UserHealthReportListProperty property : sourceList) {
            List<SysUserHealthReport> list = property.getList();

            saveList.addAll(list);
        }
        saveList = saveUserHealthReport(saveList);
        List<UserHealthReportListProperty> resultList = convertToProperty(saveList);
        return resultList;
    }

    @Override
    public List<UserHealthReportListProperty> convertToProperty(List<SysUserHealthReport> healthReportList) {

        List<UserHealthReportListProperty> list = new ArrayList<>();
        HashSet<String> yearSet = healthReportList.stream().map(entity -> entity.getYear()).collect(Collectors.toCollection(HashSet::new));
        for (String year : yearSet) {
            List<SysUserHealthReport> yearList = healthReportList.stream().filter(contract -> contract.getYear().equals(year)).collect(Collectors.toList());

            UserHealthReportListProperty userHealthReportProperty = new UserHealthReportListProperty();
            userHealthReportProperty.setYear(year);
            userHealthReportProperty.setList(yearList);

            list.add(userHealthReportProperty);
        }

        List<UserHealthReportListProperty> resultList = list.stream().sorted(Comparator.comparing(UserHealthReportListProperty::getYear).reversed()).collect(Collectors.toList());

        if(resultList.size() == 0) {
            UserHealthReportListProperty property = new UserHealthReportListProperty();
            property.setYear(LocalDate.now().getYear() + "年");
            property.setList(new ArrayList<>());

            resultList.add(property);
        }
        return resultList;
    }
}
